import { Http,Util } from '@/utils';
import ProjectServiceBase from './project-service-base';

/**
 * 项目服务对象
 *
 * @export
 * @class ProjectService
 * @extends {ProjectServiceBase}
 */
export default class ProjectService extends ProjectServiceBase {

    /**
     * Creates an instance of  ProjectService.
     * 
     * @param {*} [opts={}]
     * @memberof  ProjectService
     */
    constructor(opts: any = {}) {
        super(opts);
    }

    /**
     * 获取收藏项目接口方法
     * @param context
     * @param data
     * @param isloading
     */
    public async GetStarProjects(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let tempData:any = JSON.parse(JSON.stringify(data));
        let res:any = Http.getInstance().get(`/configs/projects/default`,tempData,isloading);
        return res;
    }

    /**
     * 添加或删除收藏接口方法
     * @param context
     * @param data
     * @param isloading
     */
    public async AddOrRemoveStarProject(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let tempData:any = JSON.parse(JSON.stringify(data));
        let res:any = Http.getInstance().put(`/configs/projects/default`,tempData,isloading);
        return res;
    }


    /**
     * SaveProject接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ProjectServiceBase
     */
    public async SaveProject(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let n = Number(context.project);
        if (isNaN(n)) {
            delete context.project;
            delete data.id;
            delete data.srfparentkey;
        }
        if(context && context.project){
            // 更新
            let res:any = Http.getInstance().put(`/projects/${context.project}`, data, isloading);
            return res;
        }else{
            // 新建
            let begin = this.getNowFormatDate();
            let end = this.getNowFormatDate2();
            let days = this.getDays(begin, end);
            data.begin = begin;
            data.end = end;
            data.days = days;
            let res:any = Http.getInstance().post(`/projects/`, data, isloading);
            return res;
        }
    }


    /**
     * UpdateProjectCycle接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ProjectService
     */
    public async UpdateProjectCycle(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let res: any = { status: 200, data: {} };
        if(!(data && data.begin && data.period)) {
            if(data.end && data.begin) {
                let begin: Date = new Date(data.begin);
                let end: Date = new Date(data.end);
                data.period = Math.floor((end.getTime() - begin.getTime())/(1000 * 60 * 60 *24)) + 1;
            }else {
                return res;
            }

        }
        let begin: Date = new Date(data.begin);
        let period = parseInt(data.period);
        let days: number = 0;
        let curWeek: number = begin.getDay();
        begin.setDate(begin.getDate() + period - 1);
        for(; period > 0; period--, curWeek++) {
            curWeek = curWeek > 6 ? (curWeek - 7) : curWeek;
            if(curWeek > 0 && curWeek < 6) {
                days++;
            }
        }
        let year = begin.getFullYear();
        let month = begin.getMonth() + 1;
        let day = begin.getDate();
        Object.assign(res.data, {
            end: `${year}-${month < 10 ? ('0' + month) : month}-${day < 10 ? ('0' + day) : day}`,
            days: days
        });
        return res;
    }

    /**
     * UpdateCycle接口方法
     *
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isloading]
     * @returns {Promise<any>}
     * @memberof ProjectServiceBase
     */
    public async UpdateCycle(context: any = {},data: any = {}, isloading?: boolean): Promise<any> {
        let res: any = { status: 200, data: {} };
        if(!(data && data.begin && data.end)) {
            return res;
        }
        let begin: Date = new Date(data.begin);
        let end: Date = new Date(data.end);
        let period = Math.floor((end.getTime() - begin.getTime())/(1000 * 60 * 60 *24)) + 1;
        let days: number = 0;
        let curWeek: number = begin.getDay();
        begin.setDate(begin.getDate() + period);
        for(; period > 0; period--, curWeek++) {
            curWeek = curWeek > 6 ? (curWeek - 7) : curWeek;
            if(curWeek > 0 && curWeek < 6) {
                days++;
            }
        }

        Object.assign(res.data, {
            days: days
        });
        return res;
    }

    /**
     * 获取最近两个月的工作日
     */
    public getDays(b:any, e:any){
        let begin: Date = new Date(b);
        let end = new Date(e);
        let period = Math.floor((end.getTime() - begin.getTime())/(1000 * 60 * 60 *24)) + 1;
        let days: number = 0;
        let curWeek: number = begin.getDay();
        begin.setDate(begin.getDate() + period);
        for(; period > 0; period--, curWeek++) {
            curWeek = curWeek > 6 ? (curWeek - 7) : curWeek;
            if(curWeek > 0 && curWeek < 6) {
                days++;
            }
        }
        return days;
    }

    /**
     * 获取指定时间格式 yyyy-mm-dd
     */
    public getNowFormatDate() {
        let date = new Date();
        let seperator1 = "-";
        let seperator2 = ":";
        let month: any = date.getMonth() + 1;
        let strDate: any = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        return date.getFullYear() + seperator1 + month + seperator1 + strDate;
    }

    /**
     * 获取两个月后的日期
     */
    public getNowFormatDate2() {
        let date = new Date();
        let end = new Date(date);
        end.setDate(date.getDate() + 60);
        let seperator1 = "-";
        let month: any = end.getMonth() + 1;
        let strDate: any = end.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        return end.getFullYear() + seperator1 + month + seperator1 + strDate;
    }


}